<?php

/*
 * Co nhiem vu hien thi danh sach user
 * Them sửa xóa user
 */

namespace Admin\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;
use Admin\Entity\User;
use Zend\Db\Adapter\Adapter;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;

class UserTable extends AbstractTableGateway {

    protected $table;
    protected $adapter;
    protected $resultSetPrototype;

    public function __construct(Adapter $adapter) {
        $this->table = 'lib_users';
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
    }

    /*
     * get all users in talbe lib_users and lib_roles with conditional roleId < 4
     * @param $value
     * @return $resultSet
     */

    public function fetchAll($paginated = false, $value = 4) {
        if ($paginated) {
            $sql = new Sql($this->adapter);
            $select = $sql->select();
            $select->from($this->table)
                    ->join('lib_roles', 'lib_roles.roleID = lib_users.roleID');

            $where = new Where();
            $where->lessThan('lib_roles.roleID', $value);
            $select->where($where);
            $resultSetPrototype = new ResultSet();
            $paginatorAdapter = new DbSelect($select, $this->getAdapter(), $resultSetPrototype);
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
    }

    public function getUser($userID) {
        $userID = (int) $userID;
        $rowset = $this->select(array('userID' => $userID));
        $row = $rowset->current();
        if (!$row) {
            return null;
        }
        return $row;
    }

    public function getUserID($userName) {
        $rowset = $this->select(array('userName' => $userName));
        $row = $rowset->current();
        if (!$row)
            return null;
        else
            return $row;
    }

    public function saveUser(User $user) {
        $data = array(
            'roleID' => $user->roleID,
            'userName' => $user->userName,
            'password' => $user->password,
            'fullName' => $user->fullName,
            'lastName' => $user->lastName,
            'sex' => $user->sex,
            'birthday' => $user->birthday,
            'email' => $user->email,
            'phoneNumber' => $user->phoneNumber,
            'residentAddress' => $user->residentAddress,
            'nativePlace' => $user->nativePlace,
            'profilePhoto' => $user->profilePhoto,
        );
        $userID = (int) $user->userID;
        if ($userID == 0) {
            $this->insert($data);
            return true;
        } else {
            if ($this->getUser($userID)) {
                $this->update($data, array('userID' => $userID));
                return true;
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
        return false;
    }
    public function updatePasswordUser($userID, $newPass) {
        $this->update(array('password' => $newPass), array('userID' => $userID));
    }
    /*
     * Cập nhật các trường trong bảng cơ sở dữ liệu usersTable
     * @param: array $data, $userID
     * @result: boolean
     */
    public function updateInfoUser($data, $userID) {
         $this->update($data, array('userID' => $userID));
    }
    public function deleteUser($userID) {
        $this->delete(array('userID' => $userID));
    }
}

?>
 